﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using Digitalmes.Model.IoTManagement;
using Digitalmes.Model;

namespace Digitalmes.ViewModel.IoTManagement.CollectTaskVMs
{
    public partial class CollectTaskListVM : BasePagedListVM<CollectTask_View, CollectTaskSearcher>
    {
        
        protected override IEnumerable<IGridColumn<CollectTask_View>> InitGridHeader()
        {
            return new List<GridColumn<CollectTask_View>>{
                
                this.MakeGridHeader(x => x.CollectTask_TaskName).SetTitle(@Localizer["Page.任务名称"].Value),
                this.MakeGridHeader(x => x.CollectTask_TargetDeviceType).SetTitle(@Localizer["Page.设备类型"].Value),
                this.MakeGridHeader(x => x.CollectTask_Device).SetTitle(@Localizer["Page.设备"].Value),
                this.MakeGridHeader(x => x.CollectTask_IntervalMinutes).SetTitle(@Localizer["Page.采集频率"].Value),
                this.MakeGridHeader(x => x.CollectTask_StartTime).SetTitle(@Localizer["Page.开始时间"].Value),
                this.MakeGridHeader(x => x.CollectTask_EndTime).SetTitle(@Localizer["Page.结束时间"].Value),
                this.MakeGridHeader(x => x.CollectTask_Status).SetTitle(@Localizer["Page.任务状态"].Value),
                this.MakeGridHeader(x => x.CollectTask_CreateTime).SetTitle(@Localizer["_Admin.CreateTime"].Value),
                this.MakeGridHeader(x => x.CollectTask_UpdateTime).SetTitle(@Localizer["_Admin.UpdateTime"].Value),
                this.MakeGridHeader(x => x.CollectTask_CreateBy).SetTitle(@Localizer["_Admin.CreateBy"].Value),
                this.MakeGridHeader(x => x.CollectTask_UpdateBy).SetTitle(@Localizer["_Admin.UpdateBy"].Value),

                this.MakeGridHeaderAction(width: 200)
            };
        }

        
        public override IOrderedQueryable<CollectTask_View> GetSearchQuery()
        {
            var query = DC.Set<CollectTask>()
                
                .CheckContain(Searcher.TaskName, x=>x.TaskName)
                .CheckEqual(Searcher.TargetDeviceType, x=>x.TargetDeviceType)
                .CheckEqual(Searcher.DeviceId, x=>x.DeviceId)
                .CheckEqual(Searcher.IntervalMinutes, x=>x.IntervalMinutes)
                .CheckBetween(Searcher.StartTime?.GetStartTime(), Searcher.StartTime?.GetEndTime(), x => x.StartTime, includeMax: false)
                .CheckBetween(Searcher.EndTime?.GetStartTime(), Searcher.EndTime?.GetEndTime(), x => x.EndTime, includeMax: false)
                .CheckEqual(Searcher.Status, x=>x.Status)
                .CheckBetween(Searcher.CreateTime?.GetStartTime(), Searcher.CreateTime?.GetEndTime(), x => x.CreateTime, includeMax: false)
                .CheckBetween(Searcher.UpdateTime?.GetStartTime(), Searcher.UpdateTime?.GetEndTime(), x => x.UpdateTime, includeMax: false)
                .CheckContain(Searcher.CreateBy, x=>x.CreateBy)
                .CheckContain(Searcher.UpdateBy, x=>x.UpdateBy)
                .Select(x => new CollectTask_View
                {
				    ID = x.ID,
                    
                    CollectTask_TaskName = x.TaskName,
                    CollectTask_TargetDeviceType = x.TargetDeviceType,
                    CollectTask_Device = x.Device.DeviceCode,
                    CollectTask_IntervalMinutes = x.IntervalMinutes,
                    CollectTask_StartTime = x.StartTime,
                    CollectTask_EndTime = x.EndTime,
                    CollectTask_Status = x.Status,
                    CollectTask_CreateTime = x.CreateTime,
                    CollectTask_UpdateTime = x.UpdateTime,
                    CollectTask_CreateBy = x.CreateBy,
                    CollectTask_UpdateBy = x.UpdateBy,
                })
                .OrderBy(x => x.ID);
            return query;
        }

        
    }
    public class CollectTask_View: CollectTask
    {
        
        public string CollectTask_TaskName { get; set; }
        public MeterDeviceTypeEnum? CollectTask_TargetDeviceType { get; set; }
        public string CollectTask_Device { get; set; }
        public int? CollectTask_IntervalMinutes { get; set; }
        public DateTime? CollectTask_StartTime { get; set; }
        public DateTime? CollectTask_EndTime { get; set; }
        public TaskStatusEnum? CollectTask_Status { get; set; }
        public DateTime? CollectTask_CreateTime { get; set; }
        public DateTime? CollectTask_UpdateTime { get; set; }
        public string CollectTask_CreateBy { get; set; }
        public string CollectTask_UpdateBy { get; set; }

    }

}